﻿using Caliburn.Micro;
using myapp.Databases.DAO;
using myapp.Events.ViewModel;
using myapp.Instances;
using myappcore.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myapp.ViewModels
{
    /// <summary>
    /// Item Group List ViewModel
    /// </summary>
    public class ItemGroupListViewModel : IBaseContentViewModel, IHandle<ViewModelItemGroupSelectedEvent>
    {
        #region Attribute

        private IObservableCollection<ItemGroupViewModel> _itemGroupViewList;

        private static ItemGroupListViewModel _instance;
        #endregion

        #region Properties

        /// <summary>
        /// List of Item Group List
        /// </summary>
        public IObservableCollection<ItemGroupViewModel> ItemGroupViewList
        {
            get
            {
                if (null == _itemGroupViewList)
                {
                    _itemGroupViewList = new BindableCollection<ItemGroupViewModel>();
                }
                if (IsChanged)
                {
                    _itemGroupViewList.Clear();
                    foreach (ItemGroup groupModel in ItemGroupDao.ItemGroupList())
                    {
                        ItemGroupViewModel groupView = new ItemGroupViewModel()
                        {
                            ItemGroup = groupModel,
                            CurrentColor = MainInstance.Instance.ColorConfig.PosstionGroupNormal
                        };
                        _itemGroupViewList.Add(groupView);
                    }
                    if (_itemGroupViewList.Count > 0)
                    {
                        IsChanged = false;
                    }
                }
                return _itemGroupViewList;
            }
        }

        /// <summary>
        /// Get Instance of Item Group View Model
        /// </summary>
        public static ItemGroupListViewModel Instance
        {
            get
            {
                if (null == _instance)
                {
                    _instance = new ItemGroupListViewModel();
                    _instance.IsChanged = true;
                }
                return _instance;
            }
        }
        #endregion

        #region Constructure
        public ItemGroupListViewModel()
        {
            MainInstance.Instance.EventAggregator.Subscribe(this);
            DisplayName = "Nhóm Item";
        }

        #endregion

        #region Method
        /// <summary>
        /// Process handle selected Item group
        /// </summary>
        /// <param name="message"></param>
        public void Handle(ViewModelItemGroupSelectedEvent message)
        {
            if (message != null && message.ItemGroupViewModel != null)
            {
                message.ItemGroupViewModel.CurrentColor = MainInstance.Instance.ColorConfig.PosstionGroupSelected;

                for (int i = 0; i < _itemGroupViewList.Count; i++)
                {
                    if (_itemGroupViewList[i].ItemGroup != message.ItemGroupViewModel.ItemGroup)
                    {
                        if (_itemGroupViewList[i].CurrentColor == MainInstance.Instance.ColorConfig.PosstionGroupSelected)
                        {
                            _itemGroupViewList[i].CurrentColor = MainInstance.Instance.ColorConfig.PosstionGroupNormal;
                            break;
                        }
                    }
                }

            }
        }
        #endregion

    }
}
